package cn.hfjava.kz.controller;

import cn.dev33.satoken.util.SaResult;

import cn.hfjava.kz.entity.po.WorkType;
import cn.hfjava.kz.service.WorkTypeService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import jakarta.annotation.Resource;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.*;

import java.util.List;


/**
 * @author 29264
 */
@Slf4j
@Tag(name = "工种管理模块", description = "工种管理接口")
@RestController
@RequestMapping("/api/v1/workType")
public class WorkTypeController {


    @Resource
    private WorkTypeService workTypeService;

    @Operation(summary = "获取申报工种列表")
    @GetMapping("/list")
    public SaResult list(){
        List<WorkType> list = workTypeService.list();
        return SaResult.data(list);
    }

    @Operation(summary = "添加申报工种")
    @PostMapping("/add")
    public SaResult add(@RequestBody WorkType workType){
        workTypeService.save(workType);
        return SaResult.ok("保存申报工种成功！");
    }


    @Operation(summary = "删除申报工种",description = "根据id删除指定申报工种")
    @DeleteMapping("/delete/{id}")
    public SaResult delete(@PathVariable Integer id) {
        WorkType workType = workTypeService.getById(id);
        if (workType == null) {
            return SaResult.error("该申报工种不存在！");
        }
        workTypeService.removeById(id);
        return SaResult.ok("删除申报工种成功！");
    }

    @Operation(summary = "修改申报工种",description = "根据id更新指定申报工种")
    @PutMapping("/update/{id}")
    public SaResult update(@PathVariable Integer id,@RequestBody WorkType workType){
        WorkType jobType = workTypeService.getById(id);
        if (jobType == null) {
            return SaResult.error("该申报工种不存在！");
        }
        workTypeService.updateById(workType);
        return SaResult.ok("修改申报工种成功！");
    }
}
